'use strict'
var { initPointList } = require("../../points")
const { Plot } = require("../../plot");
var plot = new Plot(document.querySelector("#myCanvas"));
const NUM_PARTICLES = 3000
var list = initPointList(NUM_PARTICLES);

var THICKNESS = Math.pow(80, 2), DRAG = 0.95, EASE = 0.25,
    list,
    man,
    dx, dy,
    mx, my,
    d, t, f,
    i, n,
  w = 500, h = 500;
function step() {
    var LIST_len = list.length;
        if (!man) {
            t = +new Date() * 0.001;
            mx = w * 0.5 + (Math.cos(t * 2.1) * Math.cos(t * 0.9) * w * 0.45);
            my = h * 0.5 + (Math.sin(t * 3.2) * Math.tan(Math.sin(t * 0.8)) * h * 0.45);
        }
        for (i = 0; i < LIST_len; i++) {
            let p = list[i];
            var pv2 = p.vx * p.vx + p.vy * p.vy;
            //
            if (pv2 > 200) { // 能量转移阙值
                //=0.36
                p.vx = 0.8 * p.vx;
                p.vy = 0.8 * p.vy;
                var mlen = 6;
                var arr = new Array(mlen);
                var distance = new Array(mlen);
                var orgv2 = new Array(mlen);
                //  var sumd = 0;
                for (var j = 0, length2 = mlen; j < length2; j++) {
                    arr[j] = Math.floor((Math.random() * LIST_len));
                    var tem = list[arr[j]];
                    distance[j] = Math.sqrt((tem.x - p.x) * (tem.x - p.x) + (tem.y - p.y) * (tem.y - p.y));
                    orgv2[j] = tem.vx * tem.vx + tem.vy + tem.vy;

                    let t11 = Math.random() * 2;
                    let t12 = Math.sqrt(4 - t11 * t11) - 1;
                    t11 -= 1;

                    let nvx = tem.vx + t11;
                    let nvy = tem.vy + t12;
                    orgv2[j] = nvx * nvx + nvy + nvy;
                    //  sumd += distance[j];
                }
            }
            d = (dx = mx - p.x) * dx + (dy = my - p.y) * dy;
            f = -THICKNESS / d;
            if (d < THICKNESS) {
                t = Math.atan2(dy, dx);
                p.vx += f * Math.cos(t);
                p.vy += f * Math.sin(t);
            }
            p.x += (p.vx *= DRAG) //+ (p.ox - p.x) * EASE;
            p.y += (p.vy *= DRAG)// + (p.oy - p.y) * EASE;

    }
    plot.drawList(list)
    // requestAnimationFrame(step);
}

setInterval(step, 10)
// step();
